<template>
  <div>
    <el-row :gutter="15">
      <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px">
        <el-col :span="12">
          <el-form-item label="学生姓名" prop="studentName">
            <el-input v-model="formData.studentName" placeholder="请输入学生姓名" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="学号" prop="studentID">
            <el-input v-model="formData.studentID" placeholder="请输入学号" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="性别" prop="gender">
            <el-radio-group v-model="formData.gender" size="medium">
              <el-radio v-for="(item, index) in genderOptions" :key="index" :label="item.value"
                        :disabled="item.disabled">{{item.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="年级" prop="grade">
            <el-select v-model="formData.grade" placeholder="请选择年级" clearable :style="{width: '100%'}">
              <el-option v-for="(item, index) in gradeOptions" :key="index" :label="item.label"
                         :value="item.value" :disabled="item.disabled"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="班级" prop="className">
            <el-input v-model="formData.className" placeholder="请输入班级" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="宿舍号" prop="addr">
            <el-input v-model="formData.addr" placeholder="请输入宿舍号" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="电话" prop="telephone">
            <el-input v-model="formData.telephone" placeholder="请输入电话" clearable :style="{width: '100%'}">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item size="large">
            <el-button type="primary" @click="submitForm">提交</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
  </div>
</template>
<script>
export default {
  name: 'studentForm',
  components: {},
  props: [],
  data() {
    return {
      formData: {
        studentName: undefined,
        studentID: undefined,
        gender: '男',
        grade: undefined,
        className: undefined,
        addr: undefined,
        telephone: undefined
      },
      rules: {
        studentName: [{
          required: true,
          message: '请输入学生姓名',
          trigger: 'blur'
        }],
        studentID: [{
          required: true,
          message: '请输入学号',
          trigger: 'blur'
        }],
        gender: [{
          required: true,
          message: '性别不能为空',
          trigger: 'change'
        }],
        grade: [{
          required: true,
          message: '请选择年级',
          trigger: 'change'
        }],
        className: [{
          required: true,
          message: '请输入班级',
          trigger: 'blur'
        }],
        addr: [{
          required: true,
          message: '请输入宿舍号',
          trigger: 'blur'
        }],
        telephone: [{
          required: true,
          message: '请输入电话',
          trigger: 'blur'
        }]
      },
      genderOptions: [{
        'label': '男',
        'value': '男'
      }, {
        'label': '女',
        'value': '女'
      }],
      gradeOptions: [{
        'label': '2016级',
        'value': '2016级'
      }, {
        'label': '2017级',
        'value': '2017级'
      }, {
        'label': '2018级',
        'value': '2018级'
      }, {
        'label': '2019级',
        'value': '2019级'
      }, {
        'label': '2020级',
        'value': '2020级'
      }, {
        'label': '2021级',
        'value': '2021级'
      }, {
        'label': '2022级',
        'value': '2022级'
      }]
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    submitForm() {
      this.$refs['elForm'].validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('/api/student/add', this.formData)
        if (res.code !== 0) return this.$message.error(res.msg)
        this.$message.success('新增学生信息成功')
        this.resetForm()
        this.$emit('msg', false)
      })
    },
    resetForm() {
      this.$refs['elForm'].resetFields()
    }
  }
}

</script>
<style>
</style>
